package ui.prompt
{
	import events.GestureEvent;
	
	import flash.text.TextField;
	
	import global.Managers;
	
	import resources.Resources;
	
	import ui.components.Button;
	import ui.panels.AbstractPanel;
	import ui.panels.PanelType;
	
	import utils.TextUtil;
	import utils.pool.ObjectPool;

	public class PromptBox extends AbstractPanel
	{
		public static const NONE					: int = 0;
		public static const OK						: int = 1;
		public static const YES						: int = 2;
		public static const NO						: int = 4;
		public static const CANCEL					: int = 8;
		
		private static var _instance				: PromptBox;
		
		private var _txtTitle						: TextField;
		private var _txtMessage						: TextField;
		private var _buttons						: Array = [];
		private var _okHandler						: Function;
		private var _yesHandler						: Function;
		private var _noHandler						: Function;
		private var _cancelHandler					: Function;
		
		public function PromptBox()
		{
			super(PanelType.PROMPT_BOX);
			if(_instance != null) {
				throw new Error("Singleton cannot be constructed!");
			}
			
			_bg.bitmapData = Managers.resourceManager.getBitmapData(Resources.PANEL_RECIPE_DETAIL_BG);
			_bg.width = 500;
			_bg.height = 360;
			
			_txtTitle = TextUtil.getTextFieldEasy("");
			addChild(_txtTitle);
			
			_txtMessage = TextUtil.getTextFieldEasy("");
			_txtMessage.wordWrap = true;
			_txtMessage.width = panelWidth - 20;
			_txtMessage.x = 10;
			_txtMessage.y = 25;
			addChild(_txtMessage);
		}
		public static function getInstance():PromptBox {
			if(_instance == null) _instance = new PromptBox();
			return _instance;
		}
		private function okHandler(event:GestureEvent):void {
			Managers.panelManager.close(_type);
			if(_okHandler != null) _okHandler();
		}
		private function yesHandler(event:GestureEvent):void {
			Managers.panelManager.close(_type);
			if(_yesHandler != null) _yesHandler();
		}
		private function noHandler(event:GestureEvent):void {
			Managers.panelManager.close(_type);
			if(_noHandler != null) _noHandler();
		}
		private function cancelHandler(event:GestureEvent):void {
			Managers.panelManager.close(_type);
			if(_cancelHandler != null) _cancelHandler();
		}
		
		public function show(type:int,
							 msg:String,
							 title:String,
							 okHandler:Function = null,
							 yesHandler:Function = null,
							 noHandler:Function = null,
							 cancelHandler:Function = null):void {
			_okHandler = okHandler;
			_yesHandler = yesHandler;
			_noHandler = noHandler;
			_cancelHandler = cancelHandler;
			// 设置标题和文字
			_txtTitle.text = title;
			_txtMessage.text = msg;
			// 设置按钮
			var button:Button;
			while(_buttons.length > 0) {
				button = _buttons.pop() as Button;
				button.removeListener(this);
				removeChild(button);
				ObjectPool.push(button);
			}
			if((type & OK) > 0) {
				button = ObjectPool.pop(Button) as Button;
				button.title = Managers.languageManager.getLanguageText("ok");
				button.setSize(150, 40);
				button.callBack = this.okHandler;
				button.addListener(this);
				_buttons.push(button);
			}
			if((type & YES) > 0) {
				button = ObjectPool.pop(Button) as Button;
				button.title = Managers.languageManager.getLanguageText("yes");
				button.setSize(150, 40);
				button.callBack = this.yesHandler;
				button.addListener(this);
				_buttons.push(button);
			}
			if((type & NO) > 0) {
				button = ObjectPool.pop(Button) as Button;
				button.title = Managers.languageManager.getLanguageText("no");
				button.setSize(150, 40);
				button.callBack = this.noHandler;
				button.addListener(this);
				_buttons.push(button);
			}
			if((type & CANCEL) > 0) {
				button = ObjectPool.pop(Button) as Button;
				button.title = Managers.languageManager.getLanguageText("cancel");
				button.setSize(150, 40);
				button.callBack = this.cancelHandler;
				button.addListener(this);
				_buttons.push(button);
			}
			for(var i:int = 0; i < _buttons.length; i++) {
				button = _buttons[i] as Button;
				button.x = (panelWidth - (155 * _buttons.length - 5)) / 2 + 155 * i;
				button.y = panelHeight - 50;
				addChild(button);
			}
			// 打开面板
			Managers.panelManager.open(_type);
		}
		public static function attention(msg:String, title:String = ""):void {
			if(title == "") title = Managers.languageManager.getLanguageText("alert_default");
			getInstance().show(NONE, msg, title);
		}
		public static function alert(msg:String, title:String = "", okHandler:Function = null):void {
			if(title == "") title = Managers.languageManager.getLanguageText("alert_default");
			getInstance().show(OK, msg, title, okHandler);
		}
		public static function confirm(msg:String, title:String = "", okHandler:Function = null, cancelHandler:Function = null):void {
			if(title == "") title = Managers.languageManager.getLanguageText("alert_default");
			getInstance().show(OK | CANCEL, msg, title, okHandler, null, null, cancelHandler);
		}
	}
}